﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using System.Data;

namespace Web.Cms.CommentProducts
{
    public partial class CreateEditCommentProducts : System.Web.UI.Page
    {
        #region Khai báo
        private int id = 0;
        private DataSet ds = null;

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            id = ObjConvertUtility.ToInt32(Request.QueryString["Id"]);
            if (!IsPostBack)
            {

                LoadData();
            }
        }


        #region FillData
        void FillData(CommentProductsInfo info)
        {
            info.Id = id;
            info.Title = txtTitle.Text;
            info.Contents = txtContents.Text;
            info.CreateDate = txtCreateDate.SelectedDate;
            info.ProductId = int.Parse(ddrSanPham.SelectedValue);
            int status = info.Status;

            if (status == 0)
            {
                CommentProductsBL.UpdateStatus(id, 0);
            }
            else if (status == 1)
            {
                CommentProductsBL.UpdateStatus(id, 1);
            }

        }
        #endregion



        #region BindDropdownlist
        public void BindDropdownlist()
        {
            ds = CommentProductsBL.BindDropdownlist(id);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                ddrSanPham.DataSource = ds;
                ddrSanPham.DataTextField = "ProductName";
                ddrSanPham.DataValueField = "ProductId";
                ddrSanPham.DataBind();
            }
        }

        #endregion


        #region LoadData
        private void LoadData()
        {
            try
            {
                if (id > 0)
                {
                    btnUpdate.Visible = true;

                    CommentProductsInfo info = CommentProductsBL.Select(id);
                    if (info != null)
                    {
                        txtTitle.Text = info.Title;
                        txtContents.Text = info.Contents;
                        txtCreateDate.SelectedDate = info.CreateDate;
                        BindDropdownlist();

                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CommentProductsInfo info = new CommentProductsInfo();
                FillData(info);
                CommentProductsBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

    }


}